# 导入采集配置功能模块
import queue
import socket
import struct
import threading
import time

from deepdaslib import AcquisitionSetup, ChannelSetup, Measure

# 创建一个测试函数
import os
import csv

if __name__ == "__main__":

    # 创建一个全局变量作为文件存储路径，根据个人进行设置
    file_path = r"F:\deepdaq\deepdas\ot"
    # bias 数据存放文件
    bias_file_path = r"F:\deepdaq\deepdas\test.csv"
    # 创建一个采集的对象
    my_acq = AcquisitionSetup(file_path)
    # 采样率设置:8通道同步采集，暂不支持多采样率设置，返回一个采集对象的状态，如果连接成功则返回True。失败返回False.
    my_acq.sample_set(12800)
    # # 采集对象构建成功，则通道配置
    if my_acq.Status:
        # input_mode 选项：'Voltage DC', 'IEPE'
        # 测试量（Measured_Quantity） 选项：'Acceleration', 'Acoustics', 'Voltage'
        # 传感器灵敏度 sensitivity: 根据传感器实际规格配置
        # 灵敏度单位sensitivity_unit 选项: 'mv/g', 'mv/pa', 'mv/V'
        # 量程 range 选项：10, 5 (单位为V)
        CH1 = ChannelSetup(1).channel_set(input_mode='IEPE', measured_quantity='Acceleration', sensitivity=10.17,
                                          sensitivity_unit='mv/pa', channel_range=5)

        CH2 = ChannelSetup(2).channel_set(input_mode='IEPE', measured_quantity='Acceleration', sensitivity=10.17,
                                          sensitivity_unit='mv/pa', channel_range=5)
        CH3 = ChannelSetup(3).channel_set(input_mode='IEPE', measured_quantity='Acceleration', sensitivity=10.17,
                                          sensitivity_unit='mv/pa', channel_range=5)
        CH4 = ChannelSetup(4).channel_set(input_mode='IEPE', measured_quantity='Acceleration', sensitivity=10.17,
                                          sensitivity_unit='mv/pa', channel_range=5)
        CH5 = ChannelSetup(5).channel_set(input_mode='IEPE', measured_quantity='Acceleration', sensitivity=10.17,
                                          sensitivity_unit='mv/pa', channel_range=5)
        CH6 = ChannelSetup(6).channel_set(input_mode='IEPE', measured_quantity='Acceleration', sensitivity=10.17,
                                          sensitivity_unit='mv/pa', channel_range=5)
        CH7 = ChannelSetup(7).channel_set(input_mode='IEPE', measured_quantity='Acceleration', sensitivity=10.17,
                                          sensitivity_unit='mv/pa', channel_range=5)
        CH8 = ChannelSetup(8).channel_set(input_mode='IEPE', measured_quantity='Acceleration', sensitivity=10.17,
                                          sensitivity_unit='mv/pa', channel_range=5)

        while True:
            # 初始化一个测量类，用于控制数据采集,传入采集对象my_acq,目的是传入采样率，根据自己的项目命名更改
            my_measure = Measure(my_acq, CH1, CH2, CH3, CH4, CH5, CH6, CH7, CH8)
            data = my_measure.data_queue.get()
            data = data[11:-2]
            channel_data = [struct.unpack('<h', data[i + j:i + j + 2])[0] for i in range(0, len(data), 16) for j in
                            range(0, 16, 2)]
            # 创建一个写入CSV文件的对象
            with open(bias_file_path, mode='a', newline='') as new_file:
                csv_writer = csv.writer(new_file)
                # 将'second_row'作为单列数据写入
                print(channel_data[1:8])
                csv_writer.writerow(channel_data[1:8])
            duration = 2
            # 设置采集时间，本案例为1秒。
            time.sleep(duration)
            # 传输采集结束标志，结束采集
            my_measure.exit_flag.set()
            my_measure.close_socket()

    else:
        pass

